package com.tc168.cp306.b06.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.maning.mndialoglibrary.MProgressDialog;
import com.tc168.cp306.b06.MyApplication;
import com.tc168.cp306.b06.R;
import com.tc168.cp306.b06.Utils.SPUtils;
import com.tc168.cp306.b06.bean.Topticbean;
import com.tc168.cp306.b06.gen.TopticbeanDao;
import com.tc168.cp306.b06.gen.UserAnswerbeanDao;
import com.tc168.cp306.b06.iview.NoticeDialog;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JieshiActivity extends Activity {
    @BindView(R.id.banck)
    ImageView banck;
    @BindView(R.id.count)
    TextView count;
    @BindView(R.id.title_ll)
    LinearLayout titleLl;
    @BindView(R.id.shouchang)
    Button shouchang;
    @BindView(R.id.next)
    Button next;

    private List<Topticbean> list;
    private NoticeDialog mNoticeDialog;
    private TopticbeanDao topticbeanDao;
    private UserAnswerbeanDao topicAnswerbeanDao;

    private int type;
    private int num;

    private int aaa;
    private String name;
    private Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jieshi);
        ButterKnife.bind(this);
        MProgressDialog.showProgress(this);


        new Handler().postDelayed(new Runnable() {

            @Override

            public void run() {
                MProgressDialog.dismissProgress();
            }
        }, 1000);


        num = getIntent().getIntExtra("num", 0);

        aaa = num + 1;
        type = getIntent().getIntExtra("type", 0);

        name = (String) SPUtils.get(this, "name", "");
        topticbeanDao = MyApplication.getmDaoSession().getTopticbeanDao();
        topicAnswerbeanDao = MyApplication.getmDaoSessiontwo().getUserAnswerbeanDao();


        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        //获取当前时间
        date = new Date(System.currentTimeMillis());

        list = topticbeanDao.queryBuilder().where(TopticbeanDao.Properties.Toptictype.eq(type)).list();

        if (list != null) {
            count.setText(list.get(num).getJieshi());
        }


    }

    @OnClick({R.id.banck, R.id.shouchang, R.id.next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.banck:
                finish();
                break;
            case R.id.shouchang:

                showNoticeDialog();
//
//                UserAnswerbean personbeanadd = new UserAnswerbean(null, name, list.get(num).getTitle(), list.get(num).getJieshi(), true, list.get(num).getZhengque(), date.getTime());
//
//                topicAnswerbeanDao.update(personbeanadd);
//
//                ToastUtil.showLong(this,"收藏成功");

                break;
            case R.id.next:

                if (aaa < list.size()) {

                    Intent a = new Intent(JieshiActivity.this, MatterActivity.class);


                    a.putExtra("num", aaa);
                    a.putExtra("type", type);

                    startActivity(a);
                } else {
                    Toast.makeText(this, "最后一道题了", Toast.LENGTH_SHORT).show();

                }
                finish();

                break;
        }
    }

    private void showNoticeDialog() {
        if (null == mNoticeDialog) {
            mNoticeDialog = new NoticeDialog(this);
            //mNoticeDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            mNoticeDialog.setCancelable(true);
            mNoticeDialog.setClickListener(new NoticeDialog.OnNoticeDialogClickListener() {
                @Override
                public void onConfirmClick() {
                    mNoticeDialog.dismiss();
                }
            });
        }
        mNoticeDialog.show("收藏失败，未设置默认笔记本！请在菜单——>笔记本选中并设置默认的笔记本！");
    }
}
